package com.lan.ot;

import com.lan.model.Type;
import com.lan.model.Variable;
import com.lan.model.symbol.Block;

import java.util.HashMap;
import java.util.Map;

public class BlockOt implements Ot {

    private Map<Variable, Object> variables = new HashMap<>();

    private Map<String, Variable> keyVariable = new HashMap<>();

    private Block block;

    public BlockOt() {
    }

    public BlockOt(Block block) {
        this.block = block;
    }

    public Block getBlock() {
        return block;
    }

    @Override
    public Type type() {
        return Type.Block;
    }

    @Override
    public Object getValue(Variable variable) {
        return variables.get(variable);
    }

    @Override
    public Variable getVariable(String name) {
        Variable variable = keyVariable.get(name);
        if (variable == null) {
            variable = block.getVariable(name);
        }
        return variable;
    }

    @Override
    public void setValue(Variable variable, Object value) {
        keyVariable.put(variable.getName(), variable);
        variables.put(variable, value);
    }
}
